package com.byxy.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.byxy.aspect.MyLog;
import com.byxy.entity.Agency;
import com.byxy.entity.Guide;
import com.byxy.entity.Line;
import com.byxy.entity.Order;
import com.byxy.service.OrderService;

@RequestMapping(value = "/order")
@Controller
public class OrderController {
	@Resource
	private OrderService orderService;

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add(Model model) {
		model.addAttribute("lines", orderService.findLine());
		model.addAttribute("agencys", orderService.findAgency());
		model.addAttribute("guides", orderService.findGuide());
		return "order/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(Integer lineId, Integer agencyId, Integer guideId, Order order) {
		Line line = new Line();
		if (lineId == null)
			line = null;
		else
			line.setId(lineId);
		Agency agency = new Agency();
		if (agencyId == null)
			agency = null;
		else
			agency.setId(agencyId);
		Guide guide = new Guide();
		if (guideId == null)
			guide = null;
		else
			guide.setId(guideId);
		order.setAgency(agency);
		order.setGuide(guide);
		order.setLine(line);
		orderService.save(order);
		return "redirect:/order/list";
	}

	@MyLog(desc = "删除")
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String delete(int id) {
		orderService.delete(id);
		return "redirect:/order/list";
	}

	@MyLog(desc = "显示列表")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("list", orderService.listAll());
		return "order/list";
	}

	@MyLog(desc = "数据更新")
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(Model model, int id) {
		model.addAttribute("order", orderService.getById(id));
		model.addAttribute("lines", orderService.findLine());
		model.addAttribute("agencys", orderService.findAgency());
		model.addAttribute("guides", orderService.findGuide());
		return "order/update";
	}

	@MyLog(desc = "数据更新")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(Integer lineId, Integer agencyId, Integer guideId, Order order) {
		Line line = new Line();
		if (lineId == null)
			line = null;
		else
			line.setId(lineId);
		Agency agency = new Agency();
		if (agencyId == null)
			agency = null;
		else
			agency.setId(agencyId);
		Guide guide = new Guide();
		if (guideId == null)
			guide = null;
		else
			guide.setId(guideId);
		order.setAgency(agency);
		order.setGuide(guide);
		order.setLine(line);
		orderService.update(order);
		return "redirect:/order/list";
	}
}
